package com.ibm.ems.servlets;

import com.ibm.ems.dto.EventBean;
import com.ibm.ems.dto.LocationBean;
import com.ibm.ems.dto.PromotionBean;
import com.ibm.ems.dao.EventsDAO;
import com.ibm.ems.util.ConnectionUtil;
import com.ibm.ems.util.DateUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GetEvent
 */
public class GetEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Connection conn = ConnectionUtil.getConnection();
		EventsDAO edao = new EventsDAO(conn);
		System.out.println("hi modifying1");
		
		
		if (request.getParameter("create") != null) {
			System.out.println("hi modifying2");
			EventBean e = new EventBean();

			String idString = request.getParameter("eventId");
			e.setEventId(idString);

			String Name = request.getParameter("eventName");
			e.setEventName(Name);
			String d = request.getParameter("day");
			String m = request.getParameter("month");
			String y = request.getParameter("year");
			String d1 = request.getParameter("day1");
			String m1 = request.getParameter("month1");
			String y1 = request.getParameter("year1");

			String Sdate = new String(d + "/" + m + "/" + y);
			e.setStartDate(DateUtil.convertStringToDate(Sdate));

			String Edate = new String(d1 + "/" + m1 + "/" + y1);
			e.setEndDate(DateUtil.convertStringToDate(Edate));

			e.setClassSize(Integer.parseInt(request.getParameter("classSize")));
			e.setFees(Integer.parseInt(request.getParameter("fees")));

			LocationBean location = new LocationBean();
			location.setLocationId(request.getParameter("locationId"));
			e.setLocationBean(location);

			e.setUserName(request.getParameter("speakerName"));

			PromotionBean promotion = new PromotionBean();
			promotion.setProId(request.getParameter("promotionId"));
			e.setPromotionBean(promotion);

			try {
				edao.CreateEvent(e);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
			System.out.println("hi modifying12");
			
			
			
			if(request.getParameter("modify")!=null)
			{
				EventBean e=new EventBean();
				System.out.println("hi modifying");
				//EventBean mod=new EventBean();
				String id1String = request.getParameter("eventId");
				e.setEventId(id1String);

				String Name1 = request.getParameter("eventName");
				e.setEventName(Name1);
				String d12 = request.getParameter("day");
				String m12 = request.getParameter("month");
				String y12 = request.getParameter("year");
				String d2 = request.getParameter("day1");
				String m2 = request.getParameter("month1");
				String y2 = request.getParameter("year1");

				String Sdate1 = new String(d12 + "/" + m12 + "/" + y12);
				e.setStartDate(DateUtil.convertStringToDate(Sdate1));

				String Edate1 = new String(d2 + "/" + m2 + "/" + y2);
				e.setEndDate(DateUtil.convertStringToDate(Edate1));

				e.setClassSize(Integer.parseInt(request.getParameter("classSize")));
				e.setFees(Integer.parseInt(request.getParameter("fees")));

				LocationBean location1 = new LocationBean();
				location1.setLocationId(request.getParameter("locationId"));
				e.setLocationBean(location1);

				e.setUserName(request.getParameter("speakerName"));

				PromotionBean promotion1 = new PromotionBean();
				promotion1.setProId(request.getParameter("promotionId"));
				e.setPromotionBean(promotion1);

				System.out.println("hi modifying123");
				edao.modifydatabase(e);
				
					
			}
			
		
		
		ArrayList<EventBean> l = new ArrayList<EventBean>();

		edao.EventsDaoInit(l);
		ConnectionUtil.closeConnection(conn);

		RequestDispatcher rd = request.getRequestDispatcher("/EventHm.jsp");
		request.setAttribute("a", l);
		rd.forward(request, response);
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
